/*
 * $Id: setup.c 477 2008-05-12 21:26:19Z jasta $
 *
 * Copyright (C) 2008 Josh Guilfoyle <jasta@devtcg.org>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <assert.h>

#include <gtk/gtk.h>
#include <glade/glade.h>
#include <libsexy/sexy.h>

#include "../../main.h"
#include "../../utils.h"

/*****************************************************************************/

struct
{
	gboolean ui_shown;
	GtkWidget *window;
	GtkWidget *library;
	GtkWidget *use_upnp;
	GtkWidget *lastfm_username;
	GtkWidget *lastfm_password;
} setup_gui;

/*****************************************************************************/

static void setup_done(void)
{
	if (setup_gui.ui_shown == FALSE)
		return;

	assert(setup_gui.window != NULL);
	gtk_widget_destroy(setup_gui.window);

	setup_gui.ui_shown = FALSE;
	setup_gui.window = NULL;
	setup_gui.library = NULL;
	setup_gui.use_upnp = NULL;
	setup_gui.lastfm_username = NULL;
	setup_gui.lastfm_password = NULL;
}

gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer udata)
{
	setup_done();
	gtk_main_quit();
	return FALSE;
}

void on_cancel_clicked(GtkWidget *widget, gpointer udata)
{
	printf("Aborted installation at user request.\n");
	setup_done();
	gtk_main_quit();
}

void on_ok_clicked(GtkWidget *widget, gpointer udata)
{
	GSList *loclist;
	char *loc;
	gboolean use_upnp;
	const char *lastfm_username;
	const char *lastfm_password;

	loc = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(setup_gui.library));

	if (loc == NULL || g_file_test(loc, G_FILE_TEST_IS_DIR) == FALSE)
	{
		GtkWidget *error;

		error = gtk_message_dialog_new(GTK_WINDOW(setup_gui.window),
		  GTK_DIALOG_DESTROY_WITH_PARENT,
		  GTK_MESSAGE_ERROR,
		  GTK_BUTTONS_OK,
		  "The specified media folder does not exist.");

		gtk_dialog_run(GTK_DIALOG(error));
		gtk_widget_destroy(error);

		if (loc != NULL)
			g_free(loc);

		return;
	}

	use_upnp = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(setup_gui.use_upnp));
	lastfm_username = gtk_entry_get_text(GTK_ENTRY(setup_gui.lastfm_username));
	lastfm_password = gtk_entry_get_text(GTK_ENTRY(setup_gui.lastfm_password));

	loclist = g_slist_append(NULL, loc);

	gconf_client_set_list(get_gconf_client(),
	  "/apps/five/watch_folders", GCONF_VALUE_STRING, loclist, NULL);

	g_slist_free(loclist);
	g_free(loc);

	gconf_client_set_bool(get_gconf_client(),
	  "/apps/five/setup", TRUE, NULL);

	/* Close the setup window and uninitialize the globals. */
	setup_done();

	/* Display the systray icon. */
	docklet_create_and_show();

	/* Startup the main Five engine... */
	main_startup(TRUE);
}

static void guess_library_location(GtkWidget *library)
{
	char *music;

#ifdef HAS_G_GET_USER_SPECIAL_DIR
	music = g_strdup(g_get_user_special_dir(G_USER_DIRECTORY_MUSIC));
#else
	music = g_build_filename(g_get_home_dir(), "Music", NULL);
#endif

	if (music != NULL && g_file_test(music, G_FILE_TEST_IS_DIR) == TRUE)
		gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(library), music);
	else
	{
		const char *home = g_get_home_dir();

		if (home != NULL)
		{
			gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(library),
			  g_get_home_dir());
		}
	}

	g_free(music);
}

static void lastfm_signup_cb(GtkWidget *label, const char *url)
{
    char *esc;
    char *cmd;

    esc = g_shell_quote(url);
    cmd = g_strdup_printf("gnome-open %s", esc);
    g_free(esc);

    printf("Executing %s\n", cmd);
    g_spawn_command_line_async(cmd, NULL);

    g_free(cmd);
}

void setup_create_and_show(void)
{
	GladeXML *xml;
	GtkWidget *library;
	GtkWidget *hbox;
	GtkWidget *lastfm_signup;

	xml = glade_xml_new(DATADIR "/five/glade/setup.glade", NULL, NULL);
	assert(xml != NULL);

	library = glade_xml_get_widget(xml, "library_location");
	guess_library_location(library);

	hbox = glade_xml_get_widget(xml, "lastfm_placeholder");

	lastfm_signup = sexy_url_label_new();
	sexy_url_label_set_markup(SEXY_URL_LABEL(lastfm_signup), _("New user?  <a href=\"http://last.fm\">Sign up now</a>, it's free!"));
	gtk_label_set_line_wrap(GTK_LABEL(lastfm_signup), TRUE);

	g_signal_connect(G_OBJECT(lastfm_signup), "url_activated",
	  G_CALLBACK(lastfm_signup_cb), NULL);

	gtk_box_pack_start(GTK_BOX(hbox), lastfm_signup, TRUE, TRUE, 0);

	gtk_widget_show(lastfm_signup);

	setup_gui.ui_shown = TRUE;
	setup_gui.window = glade_xml_get_widget(xml, "setup");
	setup_gui.library = library;
	setup_gui.use_upnp = glade_xml_get_widget(xml, "use_upnp");
	setup_gui.lastfm_username = glade_xml_get_widget(xml, "lastfm_username");
	setup_gui.lastfm_password = glade_xml_get_widget(xml, "lastfm_password");

	glade_xml_signal_autoconnect(xml);

	/* Uhh, how do we free GladeXML? */
}
